//
//  HBButton.m
//  HBLibary
//
//  Created by Rambo on 2018/11/5.
//

#import "HBButton.h"

@implementation HBButton


#pragma mark - 扩展触摸范围

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    
    if ([self pointInside:point withEvent:event]) {
        return self;
    }
    return [super hitTest:point withEvent:event];
    
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    if (self.isHidden || self.alpha <= 0.01 || !self.userInteractionEnabled || !self.isEnabled || UIEdgeInsetsEqualToEdgeInsets(self.extendTouchEdgeInsets, UIEdgeInsetsZero)) {
        return [super pointInside:point withEvent:event];
    }
    
    
    
    CGRect rect = UIEdgeInsetsInsetRect(self.bounds, self.extendTouchEdgeInsets);
    
    rect.size.width = MAX(rect.size.width, 0);
    rect.size.height = MAX(rect.size.height, 0);
    BOOL result = CGRectContainsPoint(rect, point);
    return result;
}

#pragma mark - 改变图片的位置

- (void)layoutSubviews{
    [super layoutSubviews];
    CGSize selfSize = self.frame.size;
    [self.imageView sizeToFit];
    [self.titleLabel sizeToFit];
    CGRect imageViewRect = self.imageView.frame;
    
    CGRect titleLabelRect = self.titleLabel.frame;
    
    
    if (_imagePoint == HBButtonImagePointLeft) {
        
        if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentLeft) {
            
            imageViewRect.origin.x = self.contentEdgeInsets.left + self.imageEdgeInsets.left;
            titleLabelRect.origin.x = CGRectGetMaxX(imageViewRect) + self.titleEdgeInsets.left;
            
        }else if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight){
            
            titleLabelRect.origin.x = selfSize.width - titleLabelRect.size.width - self.contentEdgeInsets.right;
            imageViewRect.origin.x = titleLabelRect.origin.x - imageViewRect.size.width - self.titleEdgeInsets.left;
            
        }else{
            
            imageViewRect.origin.x = (selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - imageViewRect.size.width - titleLabelRect.size.width) * 0.5 + self.imageEdgeInsets.left + self.contentEdgeInsets.left;
            titleLabelRect.origin.x = CGRectGetMaxX(imageViewRect) + self.titleEdgeInsets.left;
            
        }
        
        if (self.contentVerticalAlignment == UIControlContentVerticalAlignmentTop) {
            
            imageViewRect.origin.y = self.contentEdgeInsets.top + self.imageEdgeInsets.top;
            titleLabelRect.origin.y = self.contentEdgeInsets.top + self.titleEdgeInsets.top;
            
        }else if (self.contentVerticalAlignment == UIControlContentVerticalAlignmentBottom){
            
            imageViewRect.origin.y = selfSize.height - imageViewRect.size.height + self.contentEdgeInsets.bottom;
            titleLabelRect.origin.y = selfSize.height - titleLabelRect.size.height + self.contentEdgeInsets.bottom;
            
        }else{
            
            imageViewRect.origin.y = (selfSize.height - self.contentEdgeInsets.top - self.contentEdgeInsets.bottom - imageViewRect.size.height)* 0.5 + self.imageEdgeInsets.top + self.contentEdgeInsets.top;
            titleLabelRect.origin.y = (selfSize.height - self.contentEdgeInsets.top - self.contentEdgeInsets.bottom - titleLabelRect.size.height) * 0.5 + self.titleEdgeInsets.top + self.contentEdgeInsets.top;
            
        }
        
        
    }else if (_imagePoint == HBButtonImagePointRight){
        
        if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentLeft) {
            
            titleLabelRect.origin.x = self.contentEdgeInsets.left + self.titleEdgeInsets.left;
            imageViewRect.origin.x = CGRectGetMaxX(titleLabelRect) + self.imageEdgeInsets.left;;
            
        }else if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight){
            imageViewRect.origin.x = selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - imageViewRect.size.width + self.imageEdgeInsets.left;
            titleLabelRect.origin.x = imageViewRect.origin.x - self.imageEdgeInsets.left - titleLabelRect.size.width + self.titleEdgeInsets.left;
            
        }else{
            titleLabelRect.origin.x = (selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - imageViewRect.size.width - titleLabelRect.size.width) * 0.5 + self.titleEdgeInsets.left + self.contentEdgeInsets.left;
            imageViewRect.origin.x = CGRectGetMaxX(titleLabelRect) + self.imageEdgeInsets.left;
        }
        
        
        if (self.contentVerticalAlignment == UIControlContentVerticalAlignmentTop) {
            imageViewRect.origin.y = self.contentEdgeInsets.top + self.imageEdgeInsets.top;
            titleLabelRect.origin.y = self.contentEdgeInsets.top + self.titleEdgeInsets.top;
        }else if (self.contentVerticalAlignment == UIControlContentVerticalAlignmentBottom){
            imageViewRect.origin.y = selfSize.height - imageViewRect.size.height + self.contentEdgeInsets.bottom;
            titleLabelRect.origin.y = selfSize.height - titleLabelRect.size.height + self.contentEdgeInsets.bottom;
        }else{
            imageViewRect.origin.y = (selfSize.height - self.contentEdgeInsets.top - self.contentEdgeInsets.bottom - imageViewRect.size.height)* 0.5 + self.imageEdgeInsets.top + self.contentEdgeInsets.top;
            titleLabelRect.origin.y = (selfSize.height - self.contentEdgeInsets.top - self.contentEdgeInsets.bottom - titleLabelRect.size.height) * 0.5 + self.titleEdgeInsets.top + self.contentEdgeInsets.top;
        }
        
    }else if (_imagePoint == HBButtonImagePointTop){
        CGFloat titleMaxWidth = selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - self.titleEdgeInsets.left;
        titleMaxWidth = MIN(titleMaxWidth, [self.titleLabel sizeThatFits:CGSizeZero].width);
        titleLabelRect.size.width = titleMaxWidth;
        if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentLeft) {
            self.titleLabel.textAlignment = NSTextAlignmentLeft;
            titleLabelRect.origin.x = self.contentEdgeInsets.left + self.titleEdgeInsets.left;
            imageViewRect.origin.x = self.contentEdgeInsets.left + self.imageEdgeInsets.left;
            
        }else if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight){
            self.titleLabel.textAlignment = NSTextAlignmentRight;
            imageViewRect.origin.x = selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - imageViewRect.size.width + self.imageEdgeInsets.left;
            titleLabelRect.origin.x = selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - titleLabelRect.size.width + self.titleEdgeInsets.left;
            
        }else{
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            imageViewRect.origin.x = (selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - imageViewRect.size.width ) * 0.5 + self.imageEdgeInsets.left + self.contentEdgeInsets.left;
            titleLabelRect.origin.x = (selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - titleLabelRect.size.width ) * 0.5 + self.titleEdgeInsets.left + self.contentEdgeInsets.left;

        }
        
        
        if (self.contentVerticalAlignment == UIControlContentVerticalAlignmentTop) {
            
            imageViewRect.origin.y = self.contentEdgeInsets.top + self.imageEdgeInsets.top;
            titleLabelRect.origin.y = CGRectGetMaxY(imageViewRect) + self.titleEdgeInsets.top;
            
        }else if (self.contentVerticalAlignment == UIControlContentVerticalAlignmentBottom){
            titleLabelRect.origin.y = selfSize.height - self.contentEdgeInsets.bottom - titleLabelRect.size.height + self.titleEdgeInsets.top;
            imageViewRect.origin.y = CGRectGetMinY(titleLabelRect) - imageViewRect.size.height + self.imageEdgeInsets.top;
        }else{
            imageViewRect.origin.y = (selfSize.height - imageViewRect.size.height - titleLabelRect.size.height - self.contentEdgeInsets.top - self.contentEdgeInsets.bottom) * 0.5 + self.imageEdgeInsets.top + self.contentEdgeInsets.top;

            titleLabelRect.origin.y = CGRectGetMaxY(imageViewRect) + self.titleEdgeInsets.top;
            
        }
        
    }else if (_imagePoint == HBButtonImagePointBottom){
        CGFloat titleMaxWidth = selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - self.titleEdgeInsets.left;
        titleMaxWidth = MIN(titleMaxWidth, [self.titleLabel sizeThatFits:CGSizeZero].width);
        titleLabelRect.size.width = titleMaxWidth;
        
        if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentLeft) {
            self.titleLabel.textAlignment = NSTextAlignmentLeft;
            titleLabelRect.origin.x = self.contentEdgeInsets.left + self.titleEdgeInsets.left;
            imageViewRect.origin.x = self.contentEdgeInsets.left + self.imageEdgeInsets.left;
            
        }else if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight){
            self.titleLabel.textAlignment = NSTextAlignmentRight;
            imageViewRect.origin.x = selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - imageViewRect.size.width + self.imageEdgeInsets.left;
            titleLabelRect.origin.x = selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - titleLabelRect.size.width + self.titleEdgeInsets.left;
            
        }else{
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            imageViewRect.origin.x = (selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - imageViewRect.size.width ) * 0.5 + self.imageEdgeInsets.left + self.contentEdgeInsets.left;
            titleLabelRect.origin.x = (selfSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right - titleLabelRect.size.width ) * 0.5 + self.titleEdgeInsets.left + self.contentEdgeInsets.left;
            
        }
        
        
        if (self.contentVerticalAlignment == UIControlContentVerticalAlignmentTop) {
            
            titleLabelRect.origin.y = self.contentEdgeInsets.top + self.titleEdgeInsets.top;
            imageViewRect.origin.y = CGRectGetMaxY(titleLabelRect) + self.imageEdgeInsets.top;
            
        }else if (self.contentVerticalAlignment == UIControlContentVerticalAlignmentBottom){
            
            imageViewRect.origin.y = selfSize.height - self.contentEdgeInsets.bottom - imageViewRect.size.height + self.imageEdgeInsets.top;
            titleLabelRect.origin.y = CGRectGetMinY(imageViewRect) - titleLabelRect.size.height + self.titleEdgeInsets.top;
            
        }else{
            titleLabelRect.origin.y = (selfSize.height - titleLabelRect.size.height - imageViewRect.size.height - self.contentEdgeInsets.top - self.contentEdgeInsets.bottom) * 0.5 + self.titleEdgeInsets.top + self.contentEdgeInsets.top;
            
            imageViewRect.origin.y = CGRectGetMaxY(titleLabelRect) + self.imageEdgeInsets.top;
        }
        
        
    }
    
    self.imageView.frame = imageViewRect;
    self.titleLabel.frame = titleLabelRect;
}

//- (void)layoutContentHorizontalAlignmentWithImageViewFrame:(CGRect*)rect{
//    CGRect frame = *rect;
//
//}

- (CGSize)intrinsicContentSize{
    
    return [self contentSize];
}

- (CGSize)sizeThatFits:(CGSize)size{
    return [self contentSize];
}

- (CGSize)contentSize{
    CGSize size = CGSizeZero;
    CGSize imageSize = [self.imageView sizeThatFits:CGSizeZero];
    CGSize titleSize = [self.titleLabel sizeThatFits:CGSizeZero];
    titleSize.width += 0.5;
    
    if (_imagePoint == HBButtonImagePointLeft) {
        size.width = self.contentEdgeInsets.left + self.contentEdgeInsets.right + imageSize.width + titleSize.width;
        size.height = self.contentEdgeInsets.top + self.contentEdgeInsets.bottom + MAX(imageSize.height, titleSize.height);
    }else if (_imagePoint == HBButtonImagePointRight){
        size.width = self.contentEdgeInsets.left + self.contentEdgeInsets.right + imageSize.width + titleSize.width;
        size.height = self.contentEdgeInsets.top + self.contentEdgeInsets.bottom + MAX(imageSize.height, titleSize.height);
    }else if (_imagePoint == HBButtonImagePointTop){
        size.width = self.contentEdgeInsets.left + self.contentEdgeInsets.right + MAX(imageSize.width, titleSize.width);
        size.height = self.contentEdgeInsets.top + self.contentEdgeInsets.bottom + imageSize.height + titleSize.height;
    }else if (_imagePoint == HBButtonImagePointBottom){
        size.width = self.contentEdgeInsets.left + self.contentEdgeInsets.right + MAX(imageSize.width, titleSize.width);
        size.height = self.contentEdgeInsets.top + self.contentEdgeInsets.bottom + imageSize.height + titleSize.height;
    }
    return size;
}

#pragma mark - set

- (void)setImagePoint:(HBButtonImagePoint)imagePoint{
    _imagePoint = imagePoint;
    [self invalidateIntrinsicContentSize];
    [self layoutIfNeeded];
}


@end
